﻿using System;
using System.Collections.ObjectModel;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.Networking;

public static class NetworkUtility
{
    [CompilerGenerated]
    private static Func<bool> <>f__am$cache1;
    private static bool isSceneLoaded;

    public static object DeseralizeParamater(string json, string typeName)
    {
        System.Type type = System.Type.GetType(typeName);
        return DeseralizeParamater(json, type);
    }

    public static object DeseralizeParamater(string json, System.Type type)
    {
        if (type.Is(typeof(NetworkBehaviour)))
        {
            uint num;
            uint.TryParse(json, out num);
            return new NetworkInstanceId(num).GetComponent(type);
        }
        return GameJsonUtility.FromJson(json, type);
    }

    public static void Destroy(GameObject go)
    {
        NetworkServer.Destroy(go);
    }

    public static void DestroyNetwork()
    {
        NetworkManager singleton = NetworkManager.singleton;
        if (singleton != null)
        {
            singleton.StopHost();
            NetworkManager.Shutdown();
            UnityEngine.Object.Destroy(singleton.gameObject);
        }
    }

    public static T GetComponent<T>(this NetworkInstanceId id) where T: Component
    {
        GameObject obj2;
        if (isServer)
        {
            obj2 = NetworkServer.FindLocalObject(id);
        }
        else
        {
            obj2 = ClientScene.FindLocalObject(id);
        }
        return ((obj2 == null) ? null : obj2.GetComponent<T>());
    }

    public static Component GetComponent(this NetworkInstanceId id, System.Type type)
    {
        GameObject obj2;
        if (isServer)
        {
            obj2 = NetworkServer.FindLocalObject(id);
        }
        else
        {
            obj2 = ClientScene.FindLocalObject(id);
        }
        return ((obj2 == null) ? null : obj2.GetComponent(type));
    }

    public static NetworkInstanceId GetNetId<T>(this T comp) where T: NetworkBehaviour
    {
        if (comp == null)
        {
            return NetworkInstanceId.Invalid;
        }
        return comp.netId;
    }

    public static bool HasAuthority(this GameObject go)
    {
        NetworkIdentity component = go.GetComponent<NetworkIdentity>();
        return ((component != null) && component.hasAuthority);
    }

    public static bool IsNetworkDynamic(Asset asset)
    {
        return ((asset != null) && (asset.GetComponent<NetworkIdentity>() != null));
    }

    public static bool IsNetworkDynamic(Instance ins)
    {
        return IsNetworkDynamic(ins.prefabName);
    }

    public static bool IsNetworkDynamic(string assetName)
    {
        if (AssetManager.IsEnvironmentObject(assetName))
        {
            return false;
        }
        return IsNetworkDynamic(AssetManager.GetAsset(assetName));
    }

    public static bool IsServer(this GameObject go)
    {
        NetworkIdentity component = go.GetComponent<NetworkIdentity>();
        return ((component != null) && component.isServer);
    }

    public static bool IsValid(this NetworkInstanceId id)
    {
        return ((id != NetworkInstanceId.Invalid) && !id.IsEmpty());
    }

    public static YieldInstruction LoadLevel(string levelName)
    {
        if (!isServer)
        {
            return null;
        }
        return IngameScene.Load(levelName);
    }

    public static YieldInstruction LoadScene(string sceneName)
    {
        if (!isServer)
        {
            return null;
        }
        isSceneLoaded = false;
        networkManager.ServerChangeScene(sceneName);
        if (<>f__am$cache1 == null)
        {
            <>f__am$cache1 = () => isSceneLoaded;
        }
        return Singleton<NetworkUtilityHelper>.Instance.StartWaitUntil(<>f__am$cache1, null);
    }

    public static string SerializeParamater(object obj)
    {
        System.Type type = obj.GetType();
        if (!(obj is UnityEngine.Object))
        {
            return GameJsonUtility.ToJsonIgnoreDefaultValue(obj);
        }
        if (obj is NetworkBehaviour)
        {
            return GameJsonUtility.ToJsonIgnoreDefaultValue((obj as NetworkBehaviour).netId.Value);
        }
        object[] args = new object[] { obj, type };
        Debug.LogErrorFormat("Could not send paramater '{0}' with type '{1}',if you want to send an Object reference, you can only send 'NetworkBehaviour'.", args);
        return null;
    }

    public static void Spawn(GameObject go)
    {
        if (go.tag != GameTags.Player)
        {
            go.SetActive(true);
            NetworkServer.Spawn(go);
        }
    }

    public static void StartHost()
    {
        NetworkServer.Reset();
        networkManager.StartHost();
    }

    public static Coroutine StartWaitUntilNetworkInstanceSpawn<T>(this MonoBehaviour starter, NetworkInstanceId id, Action<T> onSpawn) where T: Component
    {
        <StartWaitUntilNetworkInstanceSpawn>c__AnonStorey5D<T> storeyd = new <StartWaitUntilNetworkInstanceSpawn>c__AnonStorey5D<T> {
            id = id,
            onSpawn = onSpawn
        };
        return starter.StartWaitUntil(new Func<bool>(storeyd.<>m__B4), new Action(storeyd.<>m__B5));
    }

    public static YieldInstruction Warp(string levelName)
    {
        if (!isServer)
        {
            return null;
        }
        return Player.local.Warp(levelName);
    }

    public static ReadOnlyCollection<NetworkConnection> connections
    {
        get
        {
            return NetworkServer.connections;
        }
    }

    public static bool isClient
    {
        get
        {
            return NetworkClient.active;
        }
    }

    public static bool isNetworkActive
    {
        get
        {
            return (isClient || isServer);
        }
    }

    public static bool isServer
    {
        get
        {
            return NetworkServer.active;
        }
    }

    public static bool isSinglePlayer
    {
        get
        {
            return ((networkManager != null) && (networkManager.numPlayers == 1));
        }
    }

    public static NetworkManager networkManager
    {
        get
        {
            return NetworkManager.singleton;
        }
    }

    [CompilerGenerated]
    private sealed class <StartWaitUntilNetworkInstanceSpawn>c__AnonStorey5D<T> where T: Component
    {
        internal NetworkInstanceId id;
        internal Action<T> onSpawn;

        internal bool <>m__B4()
        {
            return this.id.GetComponent<T>();
        }

        internal void <>m__B5()
        {
            this.onSpawn(this.id.GetComponent<T>());
        }
    }
}

